<template>
  <mu-paper :id='id' class="dashboard-chart-wrap" :class='className' :style="{height,width}">
    
  </mu-paper>

  
</template>

<script>
import echarts from 'echarts'
import { debounce } from '@/utils'
export default {
  props: {
    height: {
      type: String,
      default: '350px'
    },
    width: {
      type: String,
      default: '100%'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    className: {
      type: String,
      default: 'chart-wrap'
    },
    chartData: {
      type: Object,
      require: true
    },
    id: {
      type: String,
      default: 'chart'
    },
  },
  data () {
    return {
      chart: null
    }
  },
  mounted () {
    
    this.initChart()

    if(!this.autoResize) return 

     if (this.autoResize) {
      this.__resizeHanlder = debounce(() => {
        if (this.chart) {
          this.chart.resize()
        }
      }, 100)
      window.addEventListener('resize', this.__resizeHanlder)
    }

    // 监听侧边栏的变化
    const sidebarElm = document.getElementsByClassName('content-left')[0]
    sidebarElm.addEventListener('transitionend', this.__resizeHanlder)
    
  },
  // 销毁组件
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    if (this.autoResize) {
      window.removeEventListener('resize', this.__resizeHanlder)
    }

    const sidebarElm = document.getElementsByClassName('content-left')[0]
    sidebarElm.removeEventListener('transitionend', this.__resizeHanlder)

    this.chart.dispose()
    this.chart = null
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  methods: {
    // 设置chart的数据
    setOptions ({ expectedData, actualData } = {}) {
      this.chart.setOption({
        backgroundColor:'#fff',
        xAxis: {
          data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
          boundaryGap: false,
          axisTick: {
            show: false
          }
        },
        grid: {
          left: 10,
          right: 10,
          bottom: 10,
          top: 30,
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          },
          padding: [5, 10]
        },
        yAxis: {
          axisTick: {
            show: false
          }
        },
        legend: {
          data: ['expected', 'actual']
        },
        series: [{
          name: 'expected', itemStyle: {
            normal: {
              color: '#FF005A',
              lineStyle: {
                color: '#FF005A',
                width: 2
              }
            }
          },
          smooth: true,
          type: 'line',
          data: expectedData,
          animationDuration: 2800,
          animationEasing: 'cubicInOut'
        },
        {
          name: 'actual',
          smooth: true,
          type: 'line',
          itemStyle: {
            normal: {
              color: '#3888fa',
              lineStyle: {
                color: '#3888fa',
                width: 2
              },
              areaStyle: {
                color: '#f3f8ff'
              }
            }
          },
          data: actualData,
          animationDuration: 2800,
          animationEasing: 'quadraticOut'
        }]
      })
    },
    // 初始化echart
    initChart () {
      this.chart = echarts.init(document.getElementById(this.id))
      this.setOptions(this.chartData)
    }
  },
}
</script>

<style scoped>
.dashboard-chart-wrap {
  margin: 20px 0;
  padding: 8px;
}
</style>